/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfazAdministrarCurso.java
 *
 * Created on 27/10/2011, 10:21:29 PM
 */
package presentacion;

import Paneles.PanelGeneral;
import business.CursoBo;
import dominio.Curso;
import java.awt.BorderLayout;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.ImageIcon;

/**
 *
 * @author SEBASTIAN
 */
public class InterfazAdministrarCurso extends javax.swing.JFrame {

    private int numeroCursosActuales = 0;
    private ImageIcon icono;

    /** Creates new form InterfazAdministrarCurso */
    public InterfazAdministrarCurso() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.SOUTH);
        icono = new ImageIcon(getClass().getResource("/Imagenes/icon.gif"));
        this.setIconImage(icono.getImage());
        cursosExistentes();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    public void cursosExistentes(){
        String existentes = "El grado " + comboGrados.getSelectedItem() + "\ntiene estos cursos:";
        numeroCursosActuales = 0;
        CursoBo bo = new CursoBo();
        Collection<Curso> c = bo.encontrarCursosPorGrado(comboGrados.getSelectedIndex());
        Iterator<Curso> it = c.iterator();
        if (c.isEmpty()) {
            jTextPane1.setText("El grado " + comboGrados.getSelectedItem() + "\nno tiene cursos creados.");
        }
        else {
            while (it.hasNext()) {
                existentes = existentes.concat("\n" + it.next().getNombreCurso());
            }
            jTextPane1.setText(existentes);
        }
        jTextPane2.setText("");
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        comboGrados = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane2 = new javax.swing.JTextPane();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jComboBox3 = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextPane3 = new javax.swing.JTextPane();
        BCrearGrados = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Administrar Grados");
        setResizable(false);

        jLabel1.setText("SELECCIONE UN GRADO");

        comboGrados.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0.\tTransición", "1.\tPrimero", "2.\tSegundo", "3.\tTercero", "4.\tCuarto", "5.\tQuinto", "6.\tSexto", "7.\tSeptimo", "8.\tOctavo", "9.\tNoveno", "10.\tDecimo", "11.\tOnce" }));
        comboGrados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboGradosActionPerformed(evt);
            }
        });

        jLabel2.setText("Cursos Actuales:");

        jButton1.setText("Crear");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1.\tUno", "2.\tDos", "3.\tTres", "4.\tCuatro", "5.\tCinco", "6.\tSeis", "7. \tSiete", "8. \tOcho", "9. \tNueve" }));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jLabel3.setText("Agregar mas cursos:");

        jLabel4.setText("Cuántos cursos desea agregar?");

        jTextPane1.setBackground(new java.awt.Color(204, 204, 204));
        jTextPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jScrollPane1.setViewportView(jTextPane1);

        jTextPane2.setBackground(new java.awt.Color(204, 204, 204));
        jTextPane2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jTextPane2.setEditable(false);
        jScrollPane2.setViewportView(jTextPane2);

        jLabel5.setText("Elimininar Cursos:");

        jLabel6.setText("Escoja cuantos cursos desea eliminar del grado: ");

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1.\tUno", "2.\tDos", "3.\tTres", "4.\tCuatro", "5.\tCinco", "6.\tSeis", "7. \tSiete", "8. \tOcho", "9. \tNueve" }));
        jComboBox3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox3ActionPerformed(evt);
            }
        });

        jButton2.setText("Eliminar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTextPane3.setBackground(new java.awt.Color(204, 204, 204));
        jTextPane3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jScrollPane3.setViewportView(jTextPane3);

        BCrearGrados.setText("Crear Grados");
        BCrearGrados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCrearGradosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(comboGrados, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49))
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel6)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jLabel5)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(3, 3, 3)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(2, 2, 2))
                                .addComponent(jLabel3))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(13, 13, 13)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(12, 12, 12)
                                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                            .addComponent(jButton2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(141, 141, 141)
                .addComponent(BCrearGrados, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane1, jScrollPane2, jScrollPane3});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jComboBox2, jComboBox3});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(comboGrados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(BCrearGrados)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(23, 23, 23))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(76, 76, 76)))
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(jButton2)))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jScrollPane1, jScrollPane2, jScrollPane3});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton2});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jComboBox2, jComboBox3});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int grado = comboGrados.getSelectedIndex();
        int numeroCursos = jComboBox2.getSelectedIndex();
        numeroCursosActuales = 0;
        if (!jTextPane2.getText().equals("") && jTextPane2.getText() != null) {
            CursoBo bo = new CursoBo();
            Collection<Curso> c = bo.encontrarCursosPorGrado(comboGrados.getSelectedIndex());
            Iterator<Curso> it = c.iterator();
            while (it.hasNext()) {
                numeroCursosActuales += 1;
                it.next();
            }
            if (grado == 0) {
                for (int i = 0; i <= numeroCursos; i++) {
                    Curso cu = bo.crearCurso(grado, "0" + String.valueOf(numeroCursosActuales + 1 + i));
                }
            }
            else {
                for (int i = 0; i <= numeroCursos; i++) {
                    Curso cu = bo.crearCurso(grado, String.valueOf(grado) + "0" + String.valueOf(numeroCursosActuales + 1 + i));
                }
            }

        }
        else {
            jTextPane2.setText("Seleccione un número");
        }


    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        int grado = comboGrados.getSelectedIndex();
        int numeroCursos = jComboBox2.getSelectedIndex();
        numeroCursosActuales = 0;
        CursoBo bo = new CursoBo();
        Collection<Curso> c = bo.encontrarCursosPorGrado(comboGrados.getSelectedIndex());
        Iterator<Curso> it = c.iterator();
        while (it.hasNext()) {
            numeroCursosActuales += 1;
            it.next();
        }
        String aCrear = "Se van a crear los cursos:";
        if (grado == 0) {
            for (int i = 0; i <= numeroCursos; i++) {
                aCrear = aCrear.concat("\n0" + String.valueOf(numeroCursosActuales + 1 + i));
            }
        }
        else {
            for (int i = 0; i <= numeroCursos; i++) {
                aCrear = aCrear.concat("\n" + String.valueOf(grado) + "0" + String.valueOf(numeroCursosActuales + 1 + i));
            }
        }

        jTextPane2.setText(aCrear);
    }//GEN-LAST:event_jComboBox2ActionPerformed

    private void comboGradosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboGradosActionPerformed
        cursosExistentes();
    }//GEN-LAST:event_comboGradosActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int grado = comboGrados.getSelectedIndex();
        int numeroCursos = jComboBox3.getSelectedIndex();
        numeroCursosActuales = 0;
        if (!jTextPane3.getText().equals("") && jTextPane3.getText() != null) {
            CursoBo bo = new CursoBo();
            Collection<Curso> c = bo.encontrarCursosPorGrado(comboGrados.getSelectedIndex());
            Iterator<Curso> it = c.iterator();
            while (it.hasNext()) {
                numeroCursosActuales += 1;
                it.next();
            }
            if ((numeroCursosActuales - numeroCursos) > 0) {
                if (grado == 0) {
                    for (int i = 0; i <= numeroCursos; i++) {
                        bo.borrarCurso("0" + String.valueOf(numeroCursosActuales - i));
                    }
                }
                else {
                    for (int i = 0; i <= numeroCursos; i++) {
                        bo.borrarCurso(String.valueOf(grado) + "0" + String.valueOf(numeroCursosActuales - i));
                    }
                }
            }
            else {
                jTextPane3.setText("No puede borrar mas de los que hay");
            }
        }
        else {
            jTextPane3.setText("Seleccione un número");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox3ActionPerformed
        int grado = comboGrados.getSelectedIndex();
        int numeroCursos = jComboBox3.getSelectedIndex();
        numeroCursosActuales = 0;
        CursoBo bo = new CursoBo();
        Collection<Curso> c = bo.encontrarCursosPorGrado(comboGrados.getSelectedIndex());
        Iterator<Curso> it = c.iterator();
        while (it.hasNext()) {
            numeroCursosActuales += 1;
            it.next();
        }

        if ((numeroCursosActuales - numeroCursos) > 0) {
            String aCrear = "Se van a eliminar los cursos:";
            if (grado == 0) {
                for (int i = 0; i <= numeroCursos; i++) {
                    aCrear = aCrear.concat("\n0" + String.valueOf(numeroCursosActuales - i));
                }
            }
            else {
                for (int i = 0; i <= numeroCursos; i++) {
                    aCrear = aCrear.concat("\n" + String.valueOf(grado) + "0" + String.valueOf(numeroCursosActuales - i));
                }
            }

            jTextPane3.setText(aCrear);
        }
        else {
            jTextPane3.setText("No puede borrar mas de los que hay");
        }

    }//GEN-LAST:event_jComboBox3ActionPerformed

    private void BCrearGradosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCrearGradosActionPerformed
        InterfazCrearGrado interfazCrearGrado = new InterfazCrearGrado();
        interfazCrearGrado.setVisible(true);
    }//GEN-LAST:event_BCrearGradosActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazAdministrarCurso().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCrearGrados;
    private javax.swing.JComboBox comboGrados;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JTextPane jTextPane2;
    private javax.swing.JTextPane jTextPane3;
    // End of variables declaration//GEN-END:variables
}
